package clue

import (
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/errcode"
	clueDto "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/clue"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/utils"
	"git.mycaigou.com/gfyx/micro-gfyx-api/service/clue"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
)

type CustomerManageController struct {
	controller.BaseController
	customerManageService clue.CustomerManageServiceIface
}

func NewCustomerManageController(customerManageService clue.CustomerManageServiceIface) *CustomerManageController {
	return &CustomerManageController{customerManageService: customerManageService}
}

func (c *CustomerManageController) RegisterRouter(g *gin.RouterGroup) {
	// 我负责的客户
	g.POST("my-responsible-customers", c.myResponsibleCustomers)
	// 导出我负责的客户
	//g.POST("/export-my-responsible-customers", c.exportMyResponsibleCustomers)
	// 全部客户
	g.POST("customers", c.customers)
	// 导出客户
	g.POST("export-customers", c.exportCustomers)
	// 客户分组备选项
	g.GET("group-option", c.groupOption)
	// 保存客户
	g.POST("save", c.save)
	// 删除客户
	g.POST("delete", c.delete)
	// 导入文件
	g.POST("import", c.importFile)
	// 导入进度查询
	g.GET("import-status", c.importStatus)
	// 导入列表
	g.GET("import-list", c.importList)
	// 导入记录编辑
	g.POST("import-edit", c.importEdit)
	// 删除导入记录
	g.POST("import-delete", c.importDelete)
	// 导入统计
	g.GET("import-count", c.importCount)
}

func (c *CustomerManageController) myResponsibleCustomers(ctx *gin.Context) {
	params := &clueDto.MyResponsibleCustomersRequest{}
	if err := ctx.ShouldBindJSON(params); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(params, err))
		return
	}
	response, err := c.customerManageService.MyResponsibleCustomers(ctx, params)
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	c.Success(ctx, response)
}

func (c *CustomerManageController) customers(ctx *gin.Context) {
	params := &clueDto.CustomerRequest{}
	if err := ctx.ShouldBindJSON(params); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(params, err))
		return
	}
	response, err := c.customerManageService.CustomerList(ctx, params)
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	c.Success(ctx, response)
}

func (c *CustomerManageController) exportCustomers(ctx *gin.Context) {
	params := &clueDto.ExportCustomerRequest{}
	if err := ctx.ShouldBindJSON(params); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(params, err))
		return
	}
	if params.Category != "" && params.Category != "all" {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "Category 导出客户列表类型错误")
		return
	}
	response, err := c.customerManageService.ExportCustomerList(ctx, params)
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	c.Success(ctx, response)
}

func (c *CustomerManageController) groupOption(ctx *gin.Context) {
	params := &clueDto.GroupOptionRequest{}
	option, err := c.customerManageService.CustomerGroupOption(ctx, params)
	if err != nil {
		stark.Logger.Errorf(ctx, "groupOption-error: %+v", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(params, err))
		return
	}
	c.Success(ctx, option)
}

func (c *CustomerManageController) save(ctx *gin.Context) {
	params := &clueDto.SaveCustomerRequest{}
	if err := ctx.ShouldBindJSON(params); err != nil {
		stark.Logger.Infof(ctx, "SaveCustomerRequest-error: %+v", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(params, err))
		return
	}
	response, err := c.customerManageService.SaveCustomer(ctx, params)
	if err != nil {
		stark.Logger.Errorf(ctx, "SaveCustomer-error: %+v, param: %+v", err, params)
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	c.Success(ctx, response)
}

func (c *CustomerManageController) importFile(ctx *gin.Context) {
	params := &clueDto.ImportFileRequest{}
	if err := ctx.ShouldBindJSON(params); err != nil {
		stark.Logger.Infof(ctx, "importFile-error: %+v", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(params, err))
		return
	}
	response, err := c.customerManageService.ImportFile(ctx, params)
	if err != nil {
		stark.Logger.Errorf(ctx, "ImportFile-error: %+v, param: %+v", err, params)
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	c.Success(ctx, response)
}

func (c *CustomerManageController) importStatus(ctx *gin.Context) {
	params := &clueDto.ImportStatusRequest{}
	response, err := c.customerManageService.ImportStatus(ctx, params)
	if err != nil {
		stark.Logger.Errorf(ctx, "ImportStatus-error: %+v, param: %+v", err, params)
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	c.Success(ctx, response)
}

func (c *CustomerManageController) importList(ctx *gin.Context) {
	params := &clueDto.ImportListRequest{}
	if err := ctx.ShouldBindQuery(params); err != nil {
		stark.Logger.Infof(ctx, "importList-error: %+v", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(params, err))
		return
	}
	response, err := c.customerManageService.ImportList(ctx, params)
	if err != nil {
		stark.Logger.Errorf(ctx, "importList-error: %+v, param: %+v", err, params)
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	c.Success(ctx, response)
}

func (c *CustomerManageController) importEdit(ctx *gin.Context) {
	params := &clueDto.ImportEditRequest{}
	if err := ctx.ShouldBindJSON(params); err != nil {
		stark.Logger.Infof(ctx, "importEdit-error: %+v", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(params, err))
		return
	}
	response, err := c.customerManageService.ImportEdit(ctx, params)
	if err != nil {
		stark.Logger.Errorf(ctx, "importEdit-error: %+v, param: %+v", err, params)
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	c.Success(ctx, response)
}

func (c *CustomerManageController) importDelete(ctx *gin.Context) {
	params := &clueDto.ImportDeleteRequest{}
	if err := ctx.ShouldBindJSON(params); err != nil {
		stark.Logger.Infof(ctx, "importDelete-error: %+v", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(params, err))
		return
	}
	response, err := c.customerManageService.ImportDelete(ctx, params)
	if err != nil {
		stark.Logger.Errorf(ctx, "importDelete-error: %+v, param: %+v", err, params)
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	c.Success(ctx, response)
}

func (c *CustomerManageController) importCount(ctx *gin.Context) {
	params := &clueDto.ImportCountRequest{}
	if err := ctx.ShouldBindQuery(params); err != nil {
		stark.Logger.Infof(ctx, "importCount-error: %+v", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(params, err))
		return
	}
	response, err := c.customerManageService.ImportCount(ctx, params)
	if err != nil {
		stark.Logger.Errorf(ctx, "importCount-error: %+v, param: %+v", err, params)
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	c.Success(ctx, response)
}

func (c *CustomerManageController) delete(ctx *gin.Context) {
	params := &clueDto.DeleteCustomerRequest{}
	if err := ctx.ShouldBindJSON(params); err != nil {
		stark.Logger.Infof(ctx, "DeleteCustomerRequest-error: %+v", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(params, err))
		return
	}
	response, err := c.customerManageService.DeleteCustomer(ctx, params)
	if err != nil {
		stark.Logger.Errorf(ctx, "DeleteCustomer-error: %+v, param: %+v", err, params)
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	c.Success(ctx, response)
}
